// 新增门店相关API
// import request from '@/utils/request.js'

// 简单的请求函数，替代request工具
const request = (config) => {
	return new Promise((resolve) => {
		// 模拟API请求
		setTimeout(() => {
			resolve({ code: 200, data: null, message: 'success' });
		}, 500);
	});
}

// 获取新增门店列表
export function getInsertStoreList(params) {
	return request({
		url: '/insert/store/list',
		method: 'GET',
		data: params
	})
}

// 获取新增门店统计
export function getInsertStoreStats(params) {
	return request({
		url: '/insert/store/stats',
		method: 'GET',
		data: params
	})
}

// 获取省份城市列表
export function getProvinceList() {
	return request({
		url: '/common/province/list',
		method: 'GET'
	})
}

// 导出新增门店数据
export function exportInsertStore(params) {
	return request({
		url: '/insert/store/export',
		method: 'POST',
		data: params
	})
}

// 模拟数据 - 获取新增门店列表
export function getMockInsertStoreList(params = {}) {
	return new Promise((resolve) => {
		setTimeout(() => {
			const { page = 1, pageSize = 20, province = '', month = '', keyword = '' } = params
			
			// 生成模拟数据
			const mockData = []
			const storeNames = ['张三', '李四', '王五', '赵六', '钱七', '孙八', '周九', '吴十']
			const storeTypes = ['新增', '续约', '升级']
			const regions = ['东部战区', '西部战区', '南部战区', '北部战区', '中部战区']
			const areas = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']
			
			// 生成当月数据
			const currentDate = new Date()
			const currentMonth = currentDate.getMonth() + 1
			const daysInMonth = new Date(currentDate.getFullYear(), currentMonth, 0).getDate()
			
			for (let i = 1; i <= 50; i++) {
				const randomDay = Math.floor(Math.random() * daysInMonth) + 1
				const storeName = storeNames[Math.floor(Math.random() * storeNames.length)]
				const storeType = storeTypes[Math.floor(Math.random() * storeTypes.length)]
				const region = regions[Math.floor(Math.random() * regions.length)]
				const area = areas[Math.floor(Math.random() * areas.length)]
				
				const item = {
					id: i,
					sequence: String(i).padStart(2, '0'),
					storeName: storeName,
					storeType: storeType,
					region: region,
					area: area,
					date: `${String(currentMonth).padStart(2, '0')}.${String(randomDay).padStart(2, '0')}`,
					fullDate: `${currentDate.getFullYear()}-${String(currentMonth).padStart(2, '0')}-${String(randomDay).padStart(2, '0')}`,
					province: province || '重庆',
					city: '重庆市',
					district: region.replace('战区', '区'),
					address: `${region.replace('战区', '区')}某某街道${i}号`,
					contactPerson: storeName,
					contactPhone: `138${String(Math.floor(Math.random() * 100000000)).padStart(8, '0')}`,
					businessLicense: `${currentDate.getFullYear()}${String(Math.floor(Math.random() * 1000000)).padStart(6, '0')}`,
					status: Math.random() > 0.1 ? '已审核' : '待审核',
					createTime: `${currentDate.getFullYear()}-${String(currentMonth).padStart(2, '0')}-${String(randomDay).padStart(2, '0')} ${String(Math.floor(Math.random() * 24)).padStart(2, '0')}:${String(Math.floor(Math.random() * 60)).padStart(2, '0')}:${String(Math.floor(Math.random() * 60)).padStart(2, '0')}`,
					remark: `${storeType}门店备注信息`
				}
				
				// 省份筛选
				if (province && item.province !== province) continue
				
				// 月份筛选
				if (month && !item.fullDate.includes(`-${String(month).padStart(2, '0')}-`)) continue
				
				// 关键词搜索
				if (keyword) {
					const searchKeyword = keyword.toLowerCase()
					if (!item.storeName.toLowerCase().includes(searchKeyword) && 
						!item.region.toLowerCase().includes(searchKeyword) &&
						!item.area.toLowerCase().includes(searchKeyword) &&
						!item.storeType.toLowerCase().includes(searchKeyword)) {
						continue
					}
				}
				
				mockData.push(item)
			}
			
			// 按日期排序（最新的在前）
			mockData.sort((a, b) => new Date(b.fullDate) - new Date(a.fullDate))
			
			// 分页处理
			const startIndex = (page - 1) * pageSize
			const endIndex = startIndex + pageSize
			const list = mockData.slice(startIndex, endIndex)
			
			resolve({
				code: 200,
				message: '获取成功',
				data: {
					list: list,
					total: mockData.length,
					page: page,
					pageSize: pageSize,
					hasMore: endIndex < mockData.length
				}
			})
		}, 600)
	})
}

// 模拟数据 - 获取新增门店统计
export function getMockInsertStoreStats(params = {}) {
	return new Promise((resolve) => {
		setTimeout(() => {
			const { province = '', month = '' } = params
			
			// 根据筛选条件生成不同的统计数据
			const baseCount = 10
			const variation = Math.floor(Math.random() * 20) - 10 // -10 到 +10 的随机变化
			
			resolve({
				code: 200,
				message: '获取成功',
				data: {
					monthlyNewStores: Math.max(1, baseCount + variation), // 本月新增门店数
					totalNewStores: Math.max(50, (baseCount + variation) * 5), // 总新增门店数
					newStoreRate: Math.max(5, Math.min(95, 15 + variation)), // 新增门店增长率
					pendingAudit: Math.max(0, Math.floor((baseCount + variation) * 0.1)), // 待审核门店数
					auditedStores: Math.max(1, Math.floor((baseCount + variation) * 0.9)), // 已审核门店数
					rejectedStores: Math.max(0, Math.floor((baseCount + variation) * 0.05)), // 已拒绝门店数
					regionStats: [
						{ region: '东部战区', count: Math.max(1, Math.floor((baseCount + variation) * 0.25)) },
						{ region: '西部战区', count: Math.max(1, Math.floor((baseCount + variation) * 0.2)) },
						{ region: '南部战区', count: Math.max(1, Math.floor((baseCount + variation) * 0.2)) },
						{ region: '北部战区', count: Math.max(1, Math.floor((baseCount + variation) * 0.2)) },
						{ region: '中部战区', count: Math.max(1, Math.floor((baseCount + variation) * 0.15)) }
					],
					typeStats: [
						{ type: '新增', count: Math.max(1, Math.floor((baseCount + variation) * 0.6)) },
						{ type: '续约', count: Math.max(1, Math.floor((baseCount + variation) * 0.3)) },
						{ type: '升级', count: Math.max(1, Math.floor((baseCount + variation) * 0.1)) }
					],
					lastUpdateTime: new Date().toLocaleString('zh-CN')
				}
			})
		}, 400)
	})
}

// 模拟数据 - 获取省份列表
export function getMockProvinceList() {
	return new Promise((resolve) => {
		setTimeout(() => {
			resolve({
				code: 200,
				message: '获取成功',
				data: [
					{ code: 'CQ', name: '重庆', cities: ['重庆市'] },
					{ code: 'BJ', name: '北京', cities: ['北京市'] },
					{ code: 'SH', name: '上海', cities: ['上海市'] },
					{ code: 'GD', name: '广东', cities: ['广州市', '深圳市', '珠海市', '东莞市'] },
					{ code: 'JS', name: '江苏', cities: ['南京市', '苏州市', '无锡市', '常州市'] },
					{ code: 'ZJ', name: '浙江', cities: ['杭州市', '宁波市', '温州市', '嘉兴市'] },
					{ code: 'SD', name: '山东', cities: ['济南市', '青岛市', '烟台市', '潍坊市'] },
					{ code: 'SC', name: '四川', cities: ['成都市', '绵阳市', '德阳市', '南充市'] }
				]
			})
		}, 300)
	})
}

// 模拟数据 - 获取门店详情
export function getMockInsertStoreDetail(id) {
	return new Promise((resolve) => {
		setTimeout(() => {
			const storeNames = ['张三', '李四', '王五', '赵六']
			const storeTypes = ['新增', '续约', '升级']
			const regions = ['东部战区', '西部战区', '南部战区', '北部战区']
			
			const randomName = storeNames[Math.floor(Math.random() * storeNames.length)]
			const randomType = storeTypes[Math.floor(Math.random() * storeTypes.length)]
			const randomRegion = regions[Math.floor(Math.random() * regions.length)]
			
			resolve({
				code: 200,
				message: '获取成功',
				data: {
					id: id,
					sequence: String(id).padStart(2, '0'),
					storeName: randomName,
					storeType: randomType,
					region: randomRegion,
					area: String.fromCharCode(65 + Math.floor(Math.random() * 8)), // A-H
					province: '重庆',
					city: '重庆市',
					district: randomRegion.replace('战区', '区'),
					address: `${randomRegion.replace('战区', '区')}某某街道${id}号`,
					contactPerson: randomName,
					contactPhone: `138${String(Math.floor(Math.random() * 100000000)).padStart(8, '0')}`,
					businessLicense: `2024${String(Math.floor(Math.random() * 1000000)).padStart(6, '0')}`,
					registrationCapital: Math.floor(Math.random() * 1000000) + 100000,
					businessScope: '零售业、批发业、餐饮服务',
					legalPerson: randomName,
					establishDate: '2024-01-15',
					status: Math.random() > 0.1 ? '已审核' : '待审核',
					auditTime: new Date().toLocaleString('zh-CN'),
					auditor: '系统管理员',
					createTime: new Date().toLocaleString('zh-CN'),
					remark: `${randomType}门店详细备注信息，包含更多业务相关内容`
				}
			})
		}, 300)
	})
}
